<template>
  <div class="products">
    <div v-for="product in products" :key="product.id" class="product-card">
      <h3>{{ product.name }}</h3>
      <p>价格: ¥{{ product.price }}</p>
      <button @click="addToCart(product)">加入购物车</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表
      products: [
        { id: 1, name: '手机', price: 1000 },
        { id: 2, name: '电脑', price: 2000 },
        { id: 3, name: '键盘', price: 200 }
      ]
    }
  },
  methods: {
    // 添加商品到购物车
    addToCart(product) {
      this.$store.dispatch('addToCart', product)
    }
  }
}
</script>

<style scoped>
.products {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
}

.product-card {
  border: 1px solid #ccc;
  padding: 15px;
  border-radius: 5px;
  min-width: 200px;
}

button {
  background-color: #4CAF50;
  color: white;
  padding: 5px 10px;
  border: none;
  cursor: pointer;
  border-radius: 3px;
}

button:hover {
  background-color: #45a049;
}
</style>